package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Customer;
import cn.wolfcode.car.business.domain.GoodsCategory;
import cn.wolfcode.car.business.domain.ZNodes;
import cn.wolfcode.car.business.query.GoodsCategoryQuery;
import cn.wolfcode.car.business.service.IGoodsCategoryService;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.bcel.generic.NEWARRAY;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/goodsCategory")
public class GoodsCategoryController {
    //模板前缀
    private static final String prefix = "business/goodsCategory/";

    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    @Autowired
    private ICustomerService iCustomerService;
    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:goodsCategory:view")
    @RequestMapping("/listPage")
    public String listPage(Model model) {
        List<GoodsCategory> list = goodsCategoryService.selectParent();
        List<ZNodes> zNodesList = new ArrayList<>();
        for (GoodsCategory goodsCategory : list) {
            ZNodes zNodes = new ZNodes(goodsCategory.getId(),0L,goodsCategory.getCategoryName(),false);
            if (goodsCategory.getParentId()!=null){
                zNodes.setPId(goodsCategory.getParentId());
                zNodes.setOpen(true);
            }
            zNodesList.add(zNodes);
        }
        model.addAttribute("zNodesList", JSON.toJSONString(zNodesList));
        model.addAttribute("parents",list);

        return prefix + "list";

    }

    @RequiresPermissions("business:goodsCategory:add")
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        List<GoodsCategory> list = goodsCategoryService.selectParent();
        model.addAttribute("parents",list);
        return prefix + "add";
    }


    @RequiresPermissions("business:goodsCategory:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        List<GoodsCategory> list = goodsCategoryService.selectParent();
        model.addAttribute("parents",list);
        model.addAttribute("goodsCategory", goodsCategoryService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:goodsCategory:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<GoodsCategory> query(GoodsCategoryQuery qo) {
        return goodsCategoryService.query(qo);
    }


    //新增
    @RequiresPermissions("business:goodsCategory:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsCategory goodsCategory) {
        goodsCategoryService.save(goodsCategory);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:goodsCategory:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(GoodsCategory goodsCategory) {
        goodsCategoryService.update(goodsCategory);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:goodsCategory:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        goodsCategoryService.deleteBatch(ids);
        return AjaxResult.success();
    }


}
